package com.mybed.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mybed.crm.mapper.DeptMapper;
import com.mybed.crm.pojo.pojo.Dept;
import com.mybed.crm.pojo.vo.Children;
import com.mybed.crm.pojo.vo.DeptAndChildren;
import com.mybed.crm.result.Result;
import com.mybed.crm.result.ResultP;
import com.mybed.crm.service.DeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 嵌套部门查询
     * @return
     */
    public Result treeselect() {
        List<Dept> list = list();
        Map<Integer, Dept> deptMap = list.stream().collect(Collectors.toMap(Dept::getDeptId, Function.identity()));
        Map<Integer,DeptAndChildren> DAC=list.stream().map(o -> {
            DeptAndChildren dac = new DeptAndChildren();
            dac.setId(o.getDeptId());
            dac.setLabel(o.getDeptName());
            return dac;
        }).collect(Collectors.toMap(DeptAndChildren::getId, Function.identity()));
        System.out.println(DAC);
        List<Children> result=new ArrayList<>();
        for (Dept dept : list) {
            if(dept.getParentId()!=0){
                DAC.get(dept.getParentId()).getChildren().add(DAC.get(dept.getDeptId()));
            }else {
                result.add(DAC.get(dept.getDeptId()));
            }
        }
        System.out.println(DAC.get(0));
        DAC.forEach((o,e)->{
            if(CollectionUtils.isEmpty(e.getChildren())){
                Integer parentId = deptMap.get(e.getId()).getParentId();
                Children children=new Children();
                BeanUtils.copyProperties(e,children);
                DAC.get(parentId).getChildren().remove(e);
                DAC.get(parentId).getChildren().add(children);
            }
        });
        return Result.success(result);
    }

    @Override
    public ResultP updateDept(Dept dept) {
        if(dept.getStatus().equals("1")){
        List<Dept> depts = deptMapper.selectList(null);
        Map<Integer, Dept> deptMap = depts.stream().collect(Collectors.toMap(Dept::getDeptId, Function.identity()));
        Set<Integer> set = deptMap.keySet();
        Boolean f = f(dept.getDeptId(), set, deptMap);
        if(!f){
            return ResultP.error("该部门包含未停用的子部门");
            }
        }
        deptMapper.updateById(dept);
        return ResultP.success();
    }

    @Override
    public ResultP deleteById(Integer deptId) {
        LambdaQueryWrapper<Dept> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getParentId,deptId);
        List<Dept> list = list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            ResultP.error("存在子级部门未删除");
        }
        return ResultP.success();
    }

    /**
     * 递归查找该部门下面所有部门
     *
     *
     */
    public Boolean f(Integer deptId, Set<Integer> set, Map<Integer, Dept> deptMap){
        for (Integer i : set) {
            if(deptMap.get(i).getParentId()==deptId){
                if(deptMap.get(i).getStatus().equals("0")){
                    return false;
                }
                f(i,set,deptMap);
            }
        }
        return true;
    }


}
